<?php
/**
 * @author Mazvv
 * @package Cities
 */
class Inout extends CRM_Controller {
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'inout';
		$this->lang->load('inout');
		$this->load->model('inout_model');
		$this->SetNewToolSize('600x300');
		$this->SetEditToolSize('600x300');
		$this->SetDetailsToolSize('600x300');
		$this->SetFindToolSize('600x300');
		
		$this->a_validation_rules['_filials_rid'] = "required";         
		$this->a_validation_rules['_currencies_rid'] = "required";              
		$this->a_validation_rules['_account_states_rid'] = "required";
		$this->a_validation_rules['summ_value'] = "required";
		$this->a_validation_rules['oper_date'] = "required";
		
		$this->a_validation_fields['_filials_rid'] = $this->lang->line('FILIAL');
		$this->a_validation_fields['_currencies_rid'] = $this->lang->line('CURRENCY'); 
		$this->a_validation_fields['_account_states_rid'] = $this->lang->line('STATE'); 
		$this->a_validation_fields['summ_value'] = $this->lang->line('SUMM');
		$this->a_validation_fields['oper_date'] = $this->lang->line('OPER_DATE');
		
		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_cif_name){
		switch ($m_cif_name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}			
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}

	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 

		$content['oper_date'] = $this->lang->line('DATE_DOC');
		$content['summ_value'] = $this->lang->line('SUMM');
		$content['filial_name'] = $this->lang->line('FILIAL');
		$content['state_name'] = $this->lang->line('STATE');
		$content['currency_code'] = $this->lang->line('CURRENCY'); 
		
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->inout_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if($content['DS'] && !$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('inout/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE'); 
			
			# { oper_date
			$content['oper_date'] = $this->RenderLabel('oper_date');
			$data = array('name'=>'oper_date', 'id'=>'oper_date', 'value'=>$this->validation->oper_date?$this->validation->oper_date:date('d.m.Y'), 'size'=>'12', 'readonly'=>'readonly');
			$content['oper_date_field'] =  form_input($data).$this->RenderCalendar('oper_date');
			# } oper_date
			
			# { summ_value
			$content['summ_value'] = $this->RenderLabel('summ_value');
			$data = array('name'=>'summ_value', 'id'=>'summ_value', 'value'=>$this->validation->summ_value?$this->validation->summ_value:'', 'size'=>'12'); 
			$content['summ_value_field'] =  form_input($data);
			# } summ_value
			
			# { _currencies_rid
			$currencies = array(' '=>'');
			foreach($this->inout_model->GetCurrenciesList() as $currency){
				$currencies[$currency->rid] = $currency->currency_code;
			}
			$content['_currencies_rid'] = $this->RenderLabel('_currencies_rid');
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $currencies, $this->validation->_currencies_rid?$this->validation->_currencies_rid:'');
			# } _currencies_rid
			
			# { _filials_rid
			$filials = array(' '=>'');
			foreach($this->inout_model->GetFilialsList() as $filial){
				$filials[$filial->rid] = $filial->filial_name;
			}
			$content['_filials_rid'] = $this->RenderLabel('_filials_rid');
			$content['_filials_rid_field'] =  form_dropdown('_filials_rid', $filials, $this->validation->_filials_rid?$this->validation->_filials_rid:'');
			# } _filials_rid

			# { _acount_states_rid
			$account_states = array(' '=>'');
			foreach($this->inout_model->GetAccountStatesList() as $account_state){
				$account_states[$account_state->rid] = $account_state->state_name;
			}
			$content['_account_states_rid'] = $this->RenderLabel('_account_states_rid');
			$content['_account_states_rid_field'] =  form_dropdown('_account_states_rid', $account_states, $this->validation->_account_states_rid?$this->validation->_account_states_rid:'');
			# } _account_states_rid
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			
			return $this->load->view('inout/create', $content, True);
		}
		if($insRid = $this->inout_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->inout_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT'); 
			
			# { oper_date
			$content['oper_date'] = $this->RenderLabel('oper_date');;
			$data = array('name'=>'oper_date', 'id'=>'oper_date', 'value'=>$this->validation->oper_date?$this->validation->oper_date:$content['DS']->oper_date, 'size'=>'12', 'readonly'=>'readonly');
			$content['oper_date_field'] =  form_input($data).$this->RenderCalendar('oper_date');
			# } oper_date
			
			# { summ_value
			$content['summ_value'] = $this->RenderLabel('summ_value');
			$data = array('name'=>'summ_value', 'id'=>'summ_value', 'value'=>$this->validation->summ_value?$this->validation->summ_value:$content['DS']->summ_value, 'size'=>'12'); 
			$content['summ_value_field'] =  form_input($data);
			# } summ_value
			
			# { _currencies_rid
			$currencies = array(' '=>'');
			foreach($this->inout_model->GetCurrenciesList() as $currency){
				$currencies[$currency->rid] = $currency->currency_code;
			}
			$content['_currencies_rid'] = $this->RenderLabel('_currencies_rid');
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $currencies, $this->validation->_currencies_rid?$this->validation->_currencies_rid:$content['DS']->_currencies_rid);
			# } _currencies_rid

			# { _filials_rid
			$filials = array(' '=>'');
			foreach($this->inout_model->GetFilialsList() as $filial){
				$filials[$filial->rid] = $filial->filial_name;
			}
			$content['_filials_rid'] = $this->RenderLabel('_filials_rid');
			$content['_filials_rid_field'] =  form_dropdown('_filials_rid', $filials, $this->validation->_filials_rid?$this->validation->_filials_rid:$content['DS']->_filials_rid);
			# } _filials_rid

			# { _acount_states_rid
			$account_states = array(' '=>'');
			foreach($this->inout_model->GetAccountStatesList() as $account_state){
				$account_states[$account_state->rid] = $account_state->state_name;
			}
			$content['_account_states_rid'] = $this->RenderLabel('_account_states_rid');
			$content['_account_states_rid_field'] =  form_dropdown('_account_states_rid', $account_states, $this->validation->_account_states_rid?$this->validation->_account_states_rid:$content['DS']->_account_states_rid);
			# } _account_states_rid
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			
			return $this->load->view('inout/edit', $content, True);
		}
		if($this->inout_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->inout_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_DETAILS')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_DETAILS'); 
			
			# { oper_date
			$content['oper_date'] = $this->lang->line('OPER_DATE');
			$data = array('name'=>'oper_date', 'id'=>'oper_date', 'value'=>$this->validation->oper_date?$this->validation->oper_date:$content['DS']->oper_date, 'size'=>'12', 'readonly'=>'readonly');
			$content['oper_date_field'] =  form_input($data);
			# } oper_date
			
			# { summ_value
			$content['summ_value'] = $this->lang->line('SUMM');
			$data = array('name'=>'summ_value', 'id'=>'summ_value', 'value'=>$this->validation->summ_value?$this->validation->summ_value:$content['DS']->summ_value, 'size'=>'12'); 
			$content['summ_value_field'] =  form_input($data);
			# } summ_value
			
			# { _currencies_rid
			$currencies = array(' '=>'');
			foreach($this->inout_model->GetCurrenciesList() as $currency){
				$currencies[$currency->rid] = $currency->currency_code;
			}
			$content['_currencies_rid'] = $this->lang->line('CURRENCY');
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $currencies, $this->validation->_currencies_rid?$this->validation->_currencies_rid:$content['DS']->_currencies_rid);
			# } _currencies_rid

			# { _filials_rid
			$filials = array(' '=>'');
			foreach($this->inout_model->GetFilialsList() as $filial){
				$filials[$filial->rid] = $filial->filial_name;
			}
			$content['_filials_rid'] = $this->RenderLabel('_filials_rid');
			$content['_filials_rid_field'] =  form_dropdown('_filials_rid', $filials, $this->validation->_filials_rid?$this->validation->_filials_rid:$content['DS']->_filials_rid);
			# } _filials_rid

			# { _acount_states_rid
			$account_states = array(' '=>'');
			foreach($this->inout_model->GetAccountStatesList() as $account_state){
				$account_states[$account_state->rid] = $account_state->state_name;
			}
			$content['_account_states_rid'] = $this->RenderLabel('_account_states_rid');
			$content['_account_states_rid_field'] =  form_dropdown('_account_states_rid', $account_states, $this->validation->_account_states_rid?$this->validation->_account_states_rid:$content['DS']->_account_states_rid);
			# } _account_states_rid
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			
			return $this->load->view('inout/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->inout_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
		if (!isset($_POST['goFind'])){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_DETAILS')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_DETAILS'); 
			
			# { oper_date_from
			$content['oper_date_from'] = $this->lang->line('OPER_DATE_FROM');
			$data = array('name'=>'oper_date_from', 'id'=>'oper_date_from', 'size'=>'12', 'readonly'=>'readonly');
			$content['oper_date_from_field'] =  form_input($data).$this->RenderCalendar('oper_date_from');
			# } oper_date_from
			
			# { oper_date_to
			$content['oper_date_to'] = $this->lang->line('OPER_DATE_TO');
			$data = array('name'=>'oper_date_to', 'id'=>'oper_date_to', 'size'=>'12', 'readonly'=>'readonly');
			$content['oper_date_to_field'] =  form_input($data).$this->RenderCalendar('oper_date_to');
			# } oper_date_to
			
			# { summ_value_from
			$content['summ_value_from'] = $this->lang->line('SUMM_FROM');
			$data = array('name'=>'summ_value_from', 'id'=>'summ_value_from', 'size'=>'12'); 
			$content['summ_value_from_field'] =  form_input($data);
			# } summ_value_from

			# { summ_value_to
			$content['summ_value_to'] = $this->lang->line('SUMM_TO');
			$data = array('name'=>'summ_value_to', 'id'=>'summ_value', 'size'=>'12'); 
			$content['summ_value_to_field'] =  form_input($data);
			# } summ_value_to
			
			# { _currencies_rid
			$currencies = array(' '=>'');
			foreach($this->inout_model->GetCurrenciesList() as $currency){
				$currencies[$currency->rid] = $currency->currency_code;
			}
			$content['_currencies_rid'] = $this->lang->line('CURRENCY');
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $currencies);
			# } _currencies_rid

			# { _filials_rid
			$filials = array(' '=>'');
			foreach($this->inout_model->GetFilialsList() as $filial){
				$filials[$filial->rid] = $filial->filial_name;
			}
			$content['_filials_rid'] = $this->lang->line('FILIAL');
			$content['_filials_rid_field'] =  form_dropdown('_filials_rid', $filials);
			# } _filials_rid

			# { _acount_states_rid
			$account_states = array(' '=>'');
			foreach($this->inout_model->GetAccountStatesList() as $account_state){
				$account_states[$account_state->rid] = $account_state->state_name;
			}
			$content['_account_states_rid'] = $this->lang->line('STATE');
			$content['_account_states_rid_field'] =  form_dropdown('_account_states_rid', $account_states);
			# } _account_states_rid
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2'); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array(' '=>'', '0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')));
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT');
			# } modifyDT
			return $this->load->view('inout/find', $content, True);			
		}
		$searchRule = array();
		if(isset($_POST['oper_date_from']) && trim($_POST['oper_date_from'])) $searchRule['_outgoing_incoming.oper_date>='] = trim($_POST['oper_date_from']);
		if(isset($_POST['oper_date_to']) && trim($_POST['oper_date_to'])) $searchRule['_outgoing_incoming.oper_date<='] = trim($_POST['oper_date_to']);
		if(isset($_POST['summ_value_from']) && trim($_POST['summ_value_from'])) $searchRule['_outgoing_incoming.summ_value>='] = trim($_POST['summ_value_from']);
		if(isset($_POST['summ_value_to']) && trim($_POST['summ_value_to'])) $searchRule['_outgoing_incoming.summ_value<='] = trim($_POST['summ_value_to']);
		if(isset($_POST['_currencies_rid']) && trim($_POST['_currencies_rid'])) $searchRule['_outgoing_incoming._currencies_rid'] = trim($_POST['_currencies_rid']);
		if(isset($_POST['_filials_rid']) && trim($_POST['_filials_rid'])) $searchRule['_outgoing_incoming._filials_rid'] = trim($_POST['_filials_rid']);
		if(isset($_POST['_account_states_rid']) && trim($_POST['_currencies_rid'])) $searchRule['_outgoing_incoming._account_states_rid'] = trim($_POST['_account_states_rid']);
		if(isset($_POST['descr']) && trim($_POST['descr'])) $searchRule['_documents.descr'] = trim($_POST['descr']);
		if(isset($_POST['archive']) && trim($_POST['archive'])) $searchRule['_documents.archive'] = trim($_POST['archive']);
		if($searchRule){
			$this->_setsearchrule($searchRule);
		}
		if($_POST['goFind']=='2'){
			redirect('inout', 'refresh');
		}
		$this->load->view('standart/tmpl/find_success', array('identifier'=>$this->GetIdentifier()));
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
	
}

?>